/****************************************************
 * 创建人：  tanglei  
 * 创建时间: 2022/3/11/14:48
 * 项目名称：junit_test
 * 文件名称: Signal.java
 * 文件描述: @Description: 
 *
 * All rights Reserved, Designed By 投资交易团队
 * @Copyright:2016-2022
 *
 ********************************************************/
package com.springboot.thread;

/**
 * 包名称：  src.multiThread
 * 类名称：Signal
 * 类描述：
 * 创建人：  tanglei
 * 创建时间：2022/3/11 14:48
 */
public class Signal {

    private static volatile int signal = 0;

    static class ThreadA implements Runnable {
        @Override
        public void run() {
            while (signal < 5) {
                if (signal % 2 == 0) {
                    System.out.println("threadA: " + signal);
                    synchronized (this) {
                        signal++;
                    }
                }
            }
        }
    }
    static class ThreadB implements Runnable {
        @Override
        public void run() {
            while (signal < 5) {
                if (signal % 2 == 1) {
                    System.out.println("threadB: " + signal);
                    synchronized (this) {
                        signal++;
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new ThreadA()).start();
        Thread.sleep(1000);
        new Thread(new ThreadB()).start();
    }

}

